<?php defined('BASEPATH') or exit('No direct script access allowed');
class Bet_m extends L_Model
{
	
	public function __construct(){	
		parent::__construct();
		$this->set_table_name("bet");
		$this->load->model('channel/bet_type_m');
	}
	
	function add_bet_transaction($user_id, $bet_type_id, $channel_id, $choice, $amount) {
		$default_dbprefix = $this->db->dbprefix;
		$this->db->dbprefix='l_';
		$bet_type = $this->bet_type_m->get_by_id_with_cache($bet_type_id);
		
		$now=new DateTime();
		//FIXME: consider settle-date
		$result = $this->db->insert('l_transaction', array(
			'user_id'					=> $user_id,
			'trans_type'				=> $bet_type->bet_type,
			'trans_date'				=> $now->format('Y-m-d H:i:s'),
			'settle_date'				=> date('Y-m-d'),
			'choice'					=> $choice,
			'channel_id' 				=> $channel_id,
			'odds'						=> $bet_type->odds,
			'win_lose'					=> 0,
			'stake'						=> $amount,
			'commission_rate'			=> 0,
			'agent_rate'				=> 0,
			'status'					=> 'RUNNING',
			'affiliate_id' 				=> NULL,
			'affiliate_rate'			=> 0,
			'affiliate_commission_rate' => 0
		));
		$this->db->dbprefix = $default_dbprefix;
		return $this->db->insert_id();
	}
}